package com.lute.model;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;

import com.lute.json.ClientNameJSON;
import com.lute.utils.DAOFactory;

@Entity
public class Client {
	
	private int id_client;
	private String name;
	private ClientProfile clientProfil;
	private List<BusinessUnit> business_units = new ArrayList<BusinessUnit>();
	private List<ClientAccountingPeriod> clientAccountingPeriods = new ArrayList<ClientAccountingPeriod>();
//	private List<ClientCategory> clientgategories = new ArrayList<ClientCategory>();
	
	@Id
	@GeneratedValue(strategy=GenerationType.AUTO)
	public int getId_client() {
		return id_client;
	}
	public void setId_client(int id_client) {
		this.id_client = id_client;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	@OneToOne(cascade=CascadeType.ALL,mappedBy="client")
	public ClientProfile getClientProfil() {
		return clientProfil;
	}
	public void setClientProfil(ClientProfile clientProfil) {
		this.clientProfil = clientProfil;
	}
	
	@OneToMany(cascade=CascadeType.ALL, mappedBy="client", fetch=FetchType.LAZY)
	public List<BusinessUnit> getBusiness_units() {
		return business_units;
	}
	public void setBusiness_units(List<BusinessUnit> business_units) {
		this.business_units = business_units;
	}
	
	@OneToMany(targetEntity=ClientAccountingPeriod.class, mappedBy="client",
			cascade=CascadeType.ALL, fetch=FetchType.LAZY)
	public List<ClientAccountingPeriod> getClientAccountingPeriods() {
		return clientAccountingPeriods;
	}
	public void setClientAccountingPeriods(
			List<ClientAccountingPeriod> clientAccountingPeriods) {
		this.clientAccountingPeriods = clientAccountingPeriods;
	}

//	@OneToMany(targetEntity=ClientCategory.class,cascade=CascadeType.ALL,mappedBy="client",fetch=FetchType.LAZY)
//	public List<ClientCategory> getClientgategories() {
//		return clientgategories;
//	}
//	public void setClientgategories(List<ClientCategory> clientgategories) {
//		this.clientgategories = clientgategories;
//	}
//	public static ArrayList<Client> getClientAdminAssigned() {
//		ArrayList<Client> clients = (ArrayList<Client>)DAOFactory.getInstance().getLuteInterface().getClientsFromDatabase();
//		ArrayList<Client> clientAdminAssigned = new ArrayList<Client>();
//		for(Client client :clients) {
//			if(! (client.getClientAdmin() == null) ) {
//				clientAdminAssigned.add(client);
//			}
//		}
//		return clientAdminAssigned;
//	}
	
	public static ArrayList<ClientNameJSON> getClientsIdAndName() {
		ArrayList<ClientNameJSON> clientNameJSON = new ArrayList<ClientNameJSON>();
		ArrayList<Client> clients = getClientsFromDB();
		for(Client client : clients) {
			ClientNameJSON clientName = new ClientNameJSON();
			clientName.setClientId(Integer.toString(client.getId_client()));
			clientName.setClientName(client.getName());
			clientNameJSON.add(clientName);
		}
		return clientNameJSON;
	}
	
	public static Client isClientRegistered(int clientId) {
		ArrayList<Client> clients = getClientsFromDB();
		for(Client client : clients) {
			if(client.getId_client() == clientId) {
				return client;
			}
		}
		return null;
	}
	
	public static Client getClientFromDB(String propertyName, int propertyValue) {
		return DAOFactory.getInstance().getLuteInterface().getClientFromDataBaseById(propertyName, propertyValue);
	}
	
	public static Client getClientFromDB(String propertyName, String propertyValue) {
		return DAOFactory.getInstance().getLuteInterface().getClientFromDataBase(propertyName, propertyValue);
	}
	
	public static ArrayList<Client> getClientsFromDB() {
		ArrayList<Client> clients = (ArrayList<Client>)DAOFactory.getInstance().getLuteInterface().getClientsFromDataBase();
		return clients;
	}
	
	public static Client getClientWithClientAccPeriodByIdFromDB(String propertyName, int propertyValue) {
		return DAOFactory.getInstance().getLuteInterface().getClientWithAccPeriodByIdFromDataBase("id_client", propertyValue);
	}
	
	public static Client getClientWithBusinessUnitsByIdFromDB(String propertyName, int propertyValue) {
		return DAOFactory.getInstance().getLuteInterface().getClientWithBusinessUnitsByIdFromDataBase("id_client", propertyValue);
	}
	
	public static void addNewClientToDB(Client client) {
		DAOFactory.getInstance().getLuteInterface().addObjectToDataBase(client);
	}
	
	public static void updateClientInDB(Client client) {
		DAOFactory.getInstance().getLuteInterface().updateObjectinDataBase(client);
	}
}
